package com.lzx.iteam;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.lzx.iteam.base.FindViewById;
import com.lzx.iteam.base.MyBaseActivity;
import com.lzx.iteam.bean.WeeklyDateBean;
import com.lzx.iteam.util.WeekUtil;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 05月 23日
 * Time: 下午2:22
 *
 * @QQ : 1234567890
 * **********************************
 */
public class WeeklyDateActivity extends MyBaseActivity implements View.OnClickListener{

    @FindViewById(id = R.id.weekly_date_lv)
    private ListView mListView;
    @FindViewById(id = R.id.title_ll_back)
    private LinearLayout mLayoutBack;
    @FindViewById(id = R.id.title_tv_center)
    private TextView mTvTitle;
    @FindViewById(id = R.id.title_tv_right)
    private TextView mTvRight;

    private int mPosition = -1;
    private long mFristDate;
    private int curYear;
    private int curMonth;
    private int curDay;
    private SimpleDateFormat sdf;
    private WeeklyDateAdapter mAdapter;
    private List<WeeklyDateBean> mData = new ArrayList<WeeklyDateBean>();
    private String[] count = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"};


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.weekly_date_layout);
    }

    @Override
    public void initListener() {
        mTvRight.setOnClickListener(this);
        mLayoutBack.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mTvTitle.setText("日期");
        mTvRight.setText("完成");
        mFristDate = getIntent().getLongExtra("fristDate",-1);
        sdf = new SimpleDateFormat("M月d日");
        Calendar c = Calendar.getInstance();
        curYear = c.get(Calendar.YEAR);
        curMonth = c.get(Calendar.MONTH) + 1;
        curDay = c.get(Calendar.DATE);

        int number = WeekUtil.getDateWeekOfYear();
        for(int i = 0;i<5;i++){
            mData.add(weekFirst(number - 1 + i));
        }
        mAdapter = new WeeklyDateAdapter();
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mFristDate = mData.get(position).getFirstDate();
                mPosition = position;
                mAdapter.notifyDataSetChanged();
            }
        });

    }


    /**
     * 获取周信息  以周五为准
     * @param number
     * @return
     */
    private String weekLast(int number){
        Date first = WeekUtil.getFirstDayOfWeek(curYear,number);
        Date last1 = WeekUtil.getLastDayOfWeek(curYear,number);
        Date last2 = WeekUtil.getLastDayOfWeek(first);
        int month = last1.getMonth() + 1;
        int monthNumber = WeekUtil.getWeekOfMonth(last1);
        String weeklyDate = String.format("（%s月第%s周）%s    %s",count[month],count[monthNumber],sdf.format(first),sdf.format(last2));
        Log.d("WeeklyMouldActivity", "number === " + number);
        Log.d("WeeklyMouldActivity","weeklyDate === " + weeklyDate);
        return weeklyDate;
    }


    /**
     * 获取周信息  以周一为准
     * @param number
     * @return
     */
    private WeeklyDateBean weekFirst(int number){
        Date first = WeekUtil.getFirstDayOfWeek(curYear,number);
        Date last = WeekUtil.getLastDayOfWeek(curYear,number);
        int month = first.getMonth() + 1;
        int monthNumber = 1;
        int dayOfWeek = WeekUtil.getWeekdayOfMonth(curYear, month);
        if (dayOfWeek == 1){
            monthNumber = first.getDate()/7 + 1;
        }else{
            monthNumber = (first.getDate() - 1)/7 + 1;
        }
        String weeklyDate = String.format("（%s月第%s周）%s—%s",count[month],count[monthNumber],sdf.format(first),sdf.format(last));
        WeeklyDateBean dateBean = new WeeklyDateBean(weeklyDate,first.getTime()/1000,monthNumber);
        return dateBean;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_tv_right:
                Intent intent = new Intent();
                intent.putExtra("weekly_date",mData.get(mPosition));
                intent.putExtra("fristDate",mData.get(mPosition).getFirstDate());
                setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.title_ll_back:
                finish();
                break;

        }
    }


    class WeeklyDateAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mActivity).inflate(R.layout.weekly_date_item_layout,null);
                holder.text = (TextView) convertView.findViewById(R.id.tv_weekly_date);
                holder.image = (ImageView) convertView.findViewById(R.id.iv_weekly_select);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.text.setText(mData.get(position).getDate());
            if (mFristDate/100 == mData.get(position).getFirstDate()/100){
                holder.image.setVisibility(View.VISIBLE);
                holder.text.setTextColor(mActivity.getResources().getColor(R.color.weekly_main_orange_line));
            }else{
                holder.image.setVisibility(View.GONE);
                holder.text.setTextColor(mActivity.getResources().getColor(R.color.schedule_content));
            }
            return convertView;
        }
    }
    class ViewHolder{
        private TextView text;
        private ImageView image;
    }

}
